package com.powergisol.gis.geom;

import com.vividsolutions.jts.geom.*;

import com.vividsolutions.jts.geom.Geometry;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.factory.CommonFactoryFinder;

import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;


import java.util.ArrayList;
import java.util.List;

public class Test {

    private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

    public static void main(String[] args) {

        Test test = new Test();
        long start = System.currentTimeMillis();
        test.createPloygon();
        long end = System.currentTimeMillis();
        System.out.println(end - start);



    }

    Point createPoint() {
        return null;
    }


    void creatLineString() {
        double[][] ploygonPoints = {{113.22770966214664,23.188410115168217}, {113.22645431360458,23.186725957091298},{113.22785594516317,23.185915441377446},{113.229310969463,23.18762928501152},{113.22770966214664,23.188410115168217}};
    }

    void createPloygon(){
        double[] ploygonPoints = {
                82.522476554000036,
                45.921479346000069,
                82.606544545000077,
                45.887068095000075,
                82.596090495000055,
                45.818681180000056,
                82.532495019000066,
                45.790368125000043,
                82.470206300000029,
                45.85439918600008,
                82.522476554000036,
                45.921479346000069
        };

        Polygon polygon = new GeoPolygon(ploygonPoints).toGeometry();

        Geometry geometry = polygon.getEnvelope();
        Coordinate[] coordinates = geometry.getCoordinates();

        for(int i=0;i<coordinates.length;i++){
            System.out.println(coordinates[i].x + "  "+coordinates[i].y);
        }


    }

    public void createCollction() {

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();

        //set global state
        builder.setName("testType");
        builder.setNamespaceURI("http://www.geotools.org/");
        builder.setSRS("EPSG:4326");


        builder.add("pointProperty", Point.class);

        //add attribute setting per attribute state
        //builder.minOccurs(0).maxOccurs(2).nillable(false).add("doubleProperty",Double.class);

        //build the type
        SimpleFeatureType featureType = builder.buildFeatureType();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);

        List<SimpleFeature> features = new ArrayList<>();


        //double[] points = {0.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,0.0};

        double[] points = {82.529445921000047, 45.891859535000037, 82.494163499000081, 45.84917216100007, 82.48719413200007, 45.800822176000054, 82.536415288000057, 45.771637951000059, 82.580845004000082, 45.797773078000034, 82.62179003600005, 45.855705943000032, 82.553838706000079, 45.83958928100003, 82.542949070000077, 45.810840642000073,82.574311222000063,45.871387019000053};

        double[] ploygonPoints = {
                82.522476554000036,
                45.921479346000069,
                82.606544545000077,
                45.887068095000075,
                82.596090495000055,
                45.818681180000056,
                82.532495019000066,
                45.790368125000043,
                82.470206300000029,
                45.85439918600008,
                82.522476554000036,
                45.921479346000069
        };

        Polygon polygon = new GeoPolygon(ploygonPoints).toGeometry();


        for (int i = 0; i < points.length; i++) {
            Point point = geometryFactory.createPoint(new Coordinate(points[i], points[++i]));

            featureBuilder.add(point);
            SimpleFeature feature = featureBuilder.buildFeature(null);

            features.add(feature);
        }

        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, features);


        System.out.println(collection.size());
        System.out.println(collection.getSchema().getAttributeDescriptors());

        SimpleFeatureIterator iterator = collection.features();

        while (iterator.hasNext()){
            SimpleFeature next = iterator.next();
            System.out.println(next.getDescriptor());
        }

        iterator.close();


        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        //ff.literal(ff.property("pointProperty"),ff.literal())
        //Filter filter = ff.dwithin(ff.property("POLYGON"), ff.literal(point), distance, uom.toString());
        //Filter filter = CQL.toFilter("attName >= 5");
        Filter filter = ff.intersects( ff.property( "pointProperty"), ff.literal( polygon ) );

        SimpleFeatureCollection subCollection = collection.subCollection(filter);

        System.out.println(subCollection.size());
    }

}
